package com.un.ebs.production.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.un.ebs.production.domain.*;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author liubo
 * @since 2020-05-25
 */
@Component
public interface ProductionOrderMapper extends BaseMapper<ProductionOrder> {

    @SqlParser(filter = true)
    @Select("SELECT ISNULL(MAX(RIGHT(code,4)),0) FROM dbo.tb_production_order WHERE CONVERT(VARCHAR(4),create_time,112)= CONVERT(VARCHAR(4),GETDATE(),112)")
    int maxCode();

    @SqlParser(filter = true)
    @Select("select * from tb_production_order where id = #{id}")
    ProductionOrder selectById(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from v_production_order_ext where id = #{id}")
    ProductionOrderExt selectExtById(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from v_production_order_item_ext where id = #{itemId}")
    ProductionOrderItemExt selectItemByItemId(@Param("itemId") String itemId);

    @SqlParser(filter = true)
    @Select("select * from v_production_order_item_ext where order_id = #{orderId}")
    List<ProductionOrderItemExt> selectItemById(@Param("orderId") String orderId);

    @SqlParser(filter = true)
    @Select("select isnull(max(serial_no_end),0) from v_production_order_item_view where tenant_id=#{tenant_id}")
    int selectMaxSerialNo(@Param("tenant_id") String tenant_id);

    @Select("select * from tb_production_order_item where order_id=#{id}")
    List<ProductionOrderItem> selectByProductionOrderId(@Param("id") String id);

    @Select("select * from tb_production_work_order where production_order_item_id IN (select id from tb_production_order_item where order_id=#{id})")
    List<ProductionWorkOrder> selectWorkOrderByProductionOrderId(@Param("id") String id);

    @Select("select * from v_production_work_order_ext where production_order_id=#{id}")
    List<ProductionWorkOrderExt> selectWorkOrderExtByProductionOrderId(@Param("id") String id);
}
